package com.loie.datacenter.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.loie.datacenter.comm.CommJSON;
import com.loie.datacenter.model.bo.CurrentUser;
import com.loie.datacenter.model.bo.ParamBO;
import com.loie.datacenter.model.bo.RoleListBO;
import com.loie.datacenter.model.bo.TokenInfoBO;
import com.loie.datacenter.service.RoleListService;
import com.loie.datacenter.util.SecureUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhl
 * @create 2021/9/11 19:55
 */
@Slf4j
@RequestMapping("/roleList")
@RestController
@AllArgsConstructor
public class RoleListController {

    private final RoleListService roleListService;
    private final RedisTemplate<String, String> redisTemplate;


    @PostMapping("/getRoleList")
    public
    CommJSON getRoleList(@RequestBody String msg, @CurrentUser TokenInfoBO tokenInfoBO) {
        CommJSON commJson = new CommJSON();
        try {

            String aesSecureKey = redisTemplate.opsForValue().get(SecureUtil.MD5Encode(String.format("[DataCenter][secretKey][%s]",
                    tokenInfoBO.getToken())));
            String mwMsg = SecureUtil.decryptAES(msg, aesSecureKey);
            ParamBO bo = JSONObject.parseObject(mwMsg, ParamBO.class);

            List<RoleListBO> roleListBOList = roleListService.getRoleList(bo);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("total", roleListService.getRoleListCount());
            jsonObject.put("list", roleListBOList);
            commJson.setSuc();
            commJson.setData(SecureUtil.encryptAES(jsonObject.toJSONString(), aesSecureKey));
        } catch (Exception e) {
            commJson.setErrorMessage("获取角色列表异常");
            commJson.setError();
            e.printStackTrace();
            log.error("RoleListController.getRoleList()===>", e);
        }
        return commJson;
    }

    @PostMapping("/addRole")
    public
    CommJSON addRole(@RequestBody String msg, @CurrentUser TokenInfoBO tokenInfoBO) {
        return updateRoleList(msg, tokenInfoBO);
    }

    @PostMapping("/updateRole")
    public
    CommJSON updateRoleList(@RequestBody String msg, @CurrentUser TokenInfoBO tokenInfoBO) {
        CommJSON commJson = new CommJSON();
        try {
            String aesSecureKey = redisTemplate.opsForValue().get(SecureUtil.MD5Encode(String.format("[DataCenter][secretKey][%s]",
                    tokenInfoBO.getToken())));
            String mwMsg = SecureUtil.decryptAES(msg, aesSecureKey);
            RoleListBO bo = JSONObject.parseObject(mwMsg, RoleListBO.class);

            if (roleListService.isExist(bo)) {
                commJson.setErrorMessage("角色已存在");
                commJson.setError();
            } else {
                roleListService.updateRoleList(bo);
                commJson.setSuc();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", bo.getId());
                jsonObject.put("createdDate", bo.getCreatedDate());
                commJson.setData(SecureUtil.encryptAES(jsonObject.toJSONString(), aesSecureKey));
            }
        } catch (Exception e) {
            commJson.setErrorMessage("更新角色异常");
            commJson.setError();
            e.printStackTrace();
            log.error("RoleListController.updateRoleList()===>", e);
        }
        return commJson;
    }

    @PostMapping("/deleteRole")
    public
    CommJSON deleteRoleList(@RequestBody String msg, @CurrentUser TokenInfoBO tokenInfoBO) {
        CommJSON commJson = new CommJSON();
        try {
            String aesSecureKey = redisTemplate.opsForValue().get(SecureUtil.MD5Encode(String.format("[DataCenter][secretKey][%s]",
                    tokenInfoBO.getToken())));
            String mwMsg = SecureUtil.decryptAES(msg, aesSecureKey);
            RoleListBO bo = JSONObject.parseObject(mwMsg, RoleListBO.class);

            if (roleListService.getUserRoleCount(bo.getId()) != 0) {
                commJson.setErrorMessage("无法删除，存在用户使用了该角色，请查看用户管理");
                commJson.setError();
            } else {
                roleListService.deleteRoleList(bo);
                commJson.setSuc();
            }

        } catch (Exception e) {
            commJson.setErrorMessage("删除用户异常");
            commJson.setError();
            e.printStackTrace();
            log.error("RoleListController.deleteRoleList()===>", e);
        }
        return commJson;
    }

}
